package org.lss.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.lss.entity.ValidateDefinition;
import org.lss.validate.Delete;
import org.lss.validate.Insert;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "Swagger3Controller",value = "Swagger3.0的controller")
@RestController
public class Swagger3Controller {

    @PostMapping("/doSwagger")
    @ApiOperation(value = "测试swagger3.0")
    public String doSwagger(@Validated(Insert.class) ValidateDefinition validateDefinition, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.getErrorCount() > 0) {
            StringBuilder builder = new StringBuilder();
            bindingResult.getAllErrors().forEach(objectError -> {
                builder.append(objectError.getDefaultMessage() + "<br/>");
            });
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition();
            return builder.toString();
        }
        return "";
    }

    @PostMapping("/doSwaggerDelete")
    @ApiOperation(value = "测试swagger3.0")
    public String doSwaggerDelete(@Validated(Delete.class)ValidateDefinition validateDefinition, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.getErrorCount() > 0) {
            StringBuilder builder = new StringBuilder();
            bindingResult.getAllErrors().forEach(objectError -> {
                builder.append(objectError.getDefaultMessage() + "<br/>");
            });
            return builder.toString();
        }
        return "";
    }
}
